package regions

import (
	"context"
	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"product/common/validator"
)

var _ RegionsModel = (*customRegionsModel)(nil)

type (
	// RegionsModel is an interface to be customized, add more methods here,
	// and implement the added methods in customRegionsModel.
	RegionsModel interface {
		regionsModel
		withSession(session sqlx.Session) RegionsModel
		FindAllByPage(ctx context.Context, page int64, pageSize int64) ([]*Regions, error)
		FindOneByCode(ctx context.Context, code string) (*Regions, error)
		FindOneByName(ctx context.Context, name string) (*Regions, error)
		Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
	}

	customRegionsModel struct {
		*defaultRegionsModel
	}
)

func (m *customRegionsModel) Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error {
	return m.conn.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
		return fn(ctx, session)
	})
}

func (m *customRegionsModel) FindOneByCode(ctx context.Context, code string) (*Regions, error) {
	sql := genFindOneByCodeSql(code).From(m.table)
	query, values, errSql := sql.ToSql()
	if errSql != nil || validator.IsEmptyString(query) {
		return nil, errSql
	}
	var region *Regions
	err := m.conn.QueryRowCtx(ctx, &region, query, values...)
	switch err {
	case nil:
		return region, nil
	case sqlx.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *customRegionsModel) FindOneByName(ctx context.Context, name string) (*Regions, error) {
	sql := genFindOneByNameSql(name).From(m.table)
	query, values, errSql := sql.ToSql()
	if errSql != nil || validator.IsEmptyString(query) {
		return nil, errSql
	}
	var region *Regions
	err := m.conn.QueryRowCtx(ctx, &region, query, values...)
	switch err {
	case nil:
		return region, nil
	case sqlx.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *customRegionsModel) FindAllByPage(ctx context.Context, page int64, pageSize int64) ([]*Regions, error) {
	sql := genFindAllByPageSql(page, pageSize).From(m.table)
	query, values, errSql := sql.ToSql()
	if errSql != nil || validator.IsEmptyString(query) {
		return nil, errSql
	}
	var regionsList []*Regions
	err := m.conn.QueryRowsCtx(ctx, &regionsList, query, values...)
	switch err {
	case nil:
		return regionsList, nil
	case sqlx.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

// NewRegionsModel returns a model for the database table.
func NewRegionsModel(conn sqlx.SqlConn) RegionsModel {
	return &customRegionsModel{
		defaultRegionsModel: newRegionsModel(conn),
	}
}

func (m *customRegionsModel) withSession(session sqlx.Session) RegionsModel {
	return NewRegionsModel(sqlx.NewSqlConnFromSession(session))
}

// ##################### 以下私有方法 #####################
// 查询所有记录
func genFindAllByPageSql(page int64, pageSize int64) squirrel.SelectBuilder {
	sql := squirrel.Select(regionsRows)
	offset := (page - 1) * pageSize
	return sql.Limit(uint64(pageSize)).Offset(uint64(offset))
}

func genFindOneByNameSql(name string) squirrel.SelectBuilder {
	sql := squirrel.Select(regionsRows).Where("Name = ?", name).Limit(1)
	return sql
}

func genFindOneByCodeSql(code string) squirrel.SelectBuilder {
	sql := squirrel.Select(regionsRows).Where("Code = ?", code).Limit(1)
	return sql
}
